$(function(){
	loadKhgl();
	loadGcgl();
    query();
})

function query(value,name){
	var json = {};
	json[name] =  value;
	$('#khgl_gcgl').datagrid({
		queryParams: json
	});
}
function save(){	
	//1.获取表单中数据
	//2.ajax传递后端
	$('#gcmc_info').form('submit', {    
	    url:"/gcmc/add",
	    onSubmit: function(param){ 
	    	//param.id = 1000;  隐藏参数
	        //验证  return false 阻止表单提交  
	    },    
	    success:function(data){   
	        //3.回调函数
	        if(data == "true"){//成功: 提示成功
	        	$.messager.alert('消息','新增成功！'); 
	        	$("#gcmc_info").form("reset");//清空表单
	        	$("#khgl_gcgl").datagrid("reload")//刷新表格
	        	$('#gcmc_window').window('close');//关闭窗口
	        }else{//失败： 提示错误 
	        	$.messager.alert('警告','新增失败！'); 
	        }
	    }    
	})
	  
}
function close(){	
	$("#gcmc_info").form("reset");//清空表单
	$('#gcmc_window').window('close');
}
//打开新增工程窗口
function openGcmc(){
	$('#gcmc_window').dialog({    
	    title: '新增项目',    
	    width: 400,    
	    height: 200,    
	    closed: false,    
	    cache: false,    	       
	    modal: true,   
	    minimizable:true,
	    maximizable:true,
	    resizable:true,	    
	    buttons:[{
			text:'保存',
			handler:function(){save()}
		},{
			text:'关闭',
			handler:function(){close()}
		}]
	});	
}
//修改工程
function deleteGcmc(gcmc_id){
	var gcmc_ids = new Array();	
	if(gcmc_id){
		gcmc_ids.push(gcmc_id);
	}else{
		var rows = $("#khgl_gcgl").datagrid("getSelections");
		if(rows && rows.length > 0){
			$(rows).each(function(i,o){
				gcmc_ids[i] = o.gcmc_id
			})			
		}
	}
	if(gcmc_ids.length==0){
		$.messager.alert('提示','请选择你要删除的工程！');
		return;
	}
	$.messager.confirm('确认','您确认想要删除记录吗？',function(r){
	    if (r){    
	    	$.post("/gcmc/delete",
	    		{"gcmc_ids":gcmc_ids}, 
	    		function(data){				    		
	    		if(data){	
	    			$.messager.alert('消息','删除成功！');
	    			$("#khgl_gcgl").datagrid("reload")//刷新表格
	    		}else{
	    			$.messager.alert('警告','删除失败！');
	    		} 
	    	});					    	
    
	    }    
	});  	
}
//初始化工程信息
function loadGcgl(){
	$('#khgl_gcgl').datagrid({    
	    url:"/gcmc/getAll",
	 /*   toolbar: [{
	    	iconCls: 'icon-add',
	    	text:"新增",
	    	handler: function(){openGcmc()}
	    },'-',{
	    	iconCls: 'icon-delete',
	    	text:"删除",
	    	handler: function(){
	    		deleteGcmc();
	    	}
	    },'-',{
	    	iconCls: 'icon-edit',
			text:"修改",
			handler: function(){
				var row = $("#khgl_gcgl").datagrid("getSelected");
				if(row){
					openUpdateGcmc(row);
				}else{
					$.messager.alert('提示','请选择你要修改的工程！'); 
				}
	    	}
	    }]*/
	});
	    
}
//修改工程
function updateGcmc(){
	var row = $("#khgl_gcgl").datagrid("getSelected");
	if(row){
		openUpdateGcmc(row);
	}else{
		$.messager.alert('提示','请选择你要修改的工程！'); 
	}
}
//初始化用户信息
function loadKhgl(){
	$('#khgl_yhgl').tree({    
	    url:"/khmc/getKhmc",
	    method:"GET",
	    loadFilter: function(data){   
	    	return convertTreeJson(data);
	    },
	    onClick:function(node){
	    	var requestUrl = node.id==0 ? "getAll":("getGcmcsByKhId/"+node.id)
	    	$('#khgl_gcgl').datagrid({    
			    url:"/gcmc/"+requestUrl
			})
	    },
	    onLoadSuccess:function(node,data){	    	
	    	console.log(node)
	    	var node = $('#khgl_yhgl').tree('find',1)
	    	$('#khgl_yhgl').tree('select',node.target)
	    }
	});
}
//显示操作区域
//显示操作区域
function showOpr(value,row,index){
	return "<button onclick='openUpdateGcmc("+JSON.stringify(row)+")'>修改</button>&nbsp;&nbsp;<button onclick='deleteGcmc("+row.gcmc_id+")'>删除</button>&nbsp;&nbsp;"
}
//打开修改工程窗口
function openUpdateGcmc(row){
	$('#gcmc_window').dialog({    
	    title: '修改工程',    
	    width: 400,    
	    height: 400,    
	    closed: true,    
	    cache: false,      
	    modal: true,
	    closable:false,
	    buttons:[{
			text:'保存',
			handler:function(){
				//1.获取表单中数据
				//2.ajax传递后端
				$('#gcmc_info').form('submit', {    
				    url:"/gcmc/update",
				    onSubmit: function(param){ 
				    	param.gcmc_id = row.gcmc_id;
				    	//param.id = 1000;  隐藏参数
				        //验证  return false 阻止表单提交  
				    },    
				    success:function(data){   
				        //3.回调函数
				        if(data == "true"){//成功: 提示成功
				         	$.messager.alert('消息','修改成功！');
				        	$("#gcmc_info").form("reset");//清空表单
				        	$("#khgl_gcgl").datagrid("reload")//刷新表格
				        	$('#gcmc_window').window('close');//关闭窗口
				        }else{//失败： 提示错误 
				        	$.messager.alert('警告','修改失败！'); 
				        }
				    }    
				}); 
			}
		},{
			text:'关闭',
			handler:function(){
				$("#gcmc_info").form("reset");//清空表单
				$('#gcmc_window').window('close');
			}
		}]
	}); 
	$('#gcmc_window').form('load',{
		gcmc_name:row.gcmc_name,
		khmc_id:row.khmc_id
	});
	$('#gcmc_window').window('open');
			
	
}